package com.tanhua.mongo.api;

import com.tanhua.api.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //通用的互动数据保存
    @Override
    public Integer save(Comment comment) {
        //-----------Comment互动数据操作：新增------------------
        //1. 查询动态数据
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        //2. 判断动态数据是否存在，如果不存在，就抛异常
        if (movement == null) {
            throw new RuntimeException("动态数据不存在，无法进行 评论、点赞、喜欢 互动操作！");
        }

        //3. 设置动态的用户id  并保存
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);

        //-----------Movement动态数据操作：互动数据进行+1操作------------------
        //4. 设置修改的条件
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //5. 设置修改的数值 根据CommentType互动类型判断，对应的互动数量+1操作
        Update update = new Update();
        //根据评论类型，1-点赞，2-评论，3-喜欢  进行加一
        if (comment.getCommentType() == 1) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == 2) {
            update.inc("commentCount", 1);
        } else if (comment.getCommentType() == 3) {
            update.inc("loveCount", 1);
        }

        //设置修改参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//returnNew:设置是否返回修改后的值

        //6 使用 findAndModify 方法，实现修改并返回结果
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        //7 返回互动的总数
        return modify.statisCount(comment.getCommentType());
    }

    //根据动态ID  分页查询评论列表
    @Override
    public List<Comment> findComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        //1 创建查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType()));

        //2 设置分页数据
        query.skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        //3 执行查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //返回结果
        return commentList;
    }

    //查询数据是否存在
    @Override
    public boolean hasLike(String movementId, Long userId, CommentType type) {
        //创建查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId)
                .and("commentType").is(type.getType()));

        //执行查询
        return mongoTemplate.exists(query, Comment.class);
    }

    //通用的互动数据  删除方法
    @Override
    public Integer delete(String movementId, Long userId, CommentType commentType) {
        //-----------Comment互动数据操作：删除------------------
        //1 根据条件删除互动数据
        Query queryComment = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId)
                .and("commentType").is(commentType.getType()));

        mongoTemplate.remove(queryComment, Comment.class);

        //-----------Movement动态数据操作：互动数据进行-1操作------------------
        //2 创建修改的条件
        Query queryMovement = Query.query(Criteria.where("id").is(new ObjectId(movementId)));

        //3 创建修改的值  删除是数量-1
        Update update = new Update();

        if (commentType.getType() == 1) {
            update.inc("likeCount", -1);

        } else if (commentType.getType() == 2) {
            update.inc("commentCount", -1);

        } else if (commentType.getType() == 3) {
            update.inc("loveCount", -1);

        }

        //4 设置修改的参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        //5 调用API实现修改，并返回修改后的值
        Movement modify = mongoTemplate.findAndModify(queryMovement, update, options, Movement.class);

        //6 返回最新的互动数量
        return modify.statisCount(commentType.getType());
    }
}
